package com.aluvfy.jdbc;

import com.aluvfy.jdbc.utils.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;

/*使用preparedStatement完成删除操作*/
public class JDBCTest12 {
    /*将real为张三的数据删除*/
    public static void main(String[] args) {
//        使用Java7的新特性 try-with-resource


        String sql = "delete from t_user where realname = ?";
//        写到try()括号中，自动释放资源
        try(Connection conn = DbUtils.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);) {
        ps.setString(1, "张三");
        int count = ps.executeUpdate();
        System.out.println("删除了" + count + "条记录");

        } catch (Exception e) {
            e.printStackTrace();
        }

        /*Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DbUtils.getConnection();
            String sql = "delete from t_user where realname = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, "张三");
            int count = ps.executeUpdate();
            System.out.println("删除了" + count + "条记录");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.close(conn, ps, null);
        }*/
    }
}
